﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;

using MINERP.DAL;
using MINERP.DAL.CustomerManage;
using MINERP.COM;
using MINERP.BLL.Common;
using MINERP.MODEL.FoundationModel;

namespace MINERP.BLL.CustomerManage
{
    public class OrderBagManageBLL : CustomerManagerBLLBase
    {

        #region //Private Fields

        private int _status = 0;

        private List<Tb_OrderBag> _collection = null;

        /// <summary>
        /// 数据库缓存
        /// </summary>
        private List<OrderBagEntity> _resultList = new List<OrderBagEntity>();

        /// <summary>
        /// 所有的流程节点(缓存起来)
        /// </summary>
        private List<TechInfo> _resultTechList = new List<TechInfo>();

        #endregion

        #region//public Priorities
        /// <summary>
        /// 设定查询状态时结果变化
        /// </summary>
        public int Status
        {
            set
            {
                if (this._status != value && value != 0)
                {
                    this._status = value;
                    _collection = OrderBagDAL.GetOrderBagByStatus(this._status);
                }
                else if (value == 0)
                {
                    _collection = OrderBagDAL.GetDefaultFirstPageData(50);
                }

                if (_resultList.Count > 0)
                {
                    _resultList.Clear();
                }

                //foreach (Tb_OrderBag bag in _collection)
                //{
                //    _resultList.Add(new OrderBagEntity(bag));
                //}
            }
        }

        /// <summary>
        /// 得到默认的查询结果
        /// </summary>
        public List<OrderBagEntity> DefaultResultList
        {
            get
            {
                return this._resultList;
            }
        }


        #endregion

        #region //Public Methods

        #region //get methods

        /// <summary>
        /// 得到订单中的其中一张照片
        /// </summary>
        /// <param name="bagID"></param>
        /// <returns></returns>
        public string GetImagePathByBagID(int bagID)
        {
            return OrderBagDAL.GetOneImagePathByBag(bagID);
        }
        public string GetImagePathByBagID_bk(int bagID)
        {
            return OrderBagDAL.GetOneImagePathByBag_bk(bagID);
        }

        /// <summary>
        /// 获取所有的停止、恢复记录
        /// </summary>
        /// <returns></returns>
        public List<StopRecord> GetStopRecords(int productId)
        {
            List<CtmOperateRecord> query = OrderBagDAL.GetStopRecords(productId);
            List<StopRecord> records = new List<StopRecord>();
            foreach (CtmOperateRecord c in query)
            {
                StopRecord s = new StopRecord();
                if (!c.orderInfoReference.IsLoaded)
                {
                    c.orderInfoReference.Load();
                }
                if (!c.orderInfo.Tb_OrderBagReference.IsLoaded)
                {
                    c.orderInfo.Tb_OrderBagReference.Load();
                }
                s.CreateTime = c.CreateTime;
                s.OrderBagCode = c.orderInfo.Tb_OrderBag.BagCode;
                s.OrderName = c.orderInfo.Tb_OrderBag.BagName;
                s.OrderProductCode = c.orderInfo.OrderProductCode;
                s.OrderProductName = c.orderInfo.OrderProductName;
                int status = c.OperateType;
                switch (status)                                 //这个要修改
                {
                    case 0:
                        s.OrderProductStatus = "停止"; break;
                    case 1:
                        s.OrderProductStatus = "恢复"; break;
                }
                s.Remark = c.Remark;
                if (!c.tb_TechReference.IsLoaded)
                {
                    c.tb_TechReference.Load();
                }
                s.TechName = c.tb_Tech.TechName;
                if (!c.usersReference.IsLoaded)
                {
                    c.usersReference.Load();
                }
                s.WorkerName = c.users.UserName;
                records.Add(s);
            }
            return records;
        }


        #region //分页
        /// <summary>
        /// 得到订单的指定页数据
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public List<OrderBagEntity> GetRequestPageData(int start, int end, int status)
        {
            List<Tb_OrderBag> list = OrderBagDAL.GetRequestPageData(start, end, status);
            List<OrderBagEntity> collection = new List<OrderBagEntity>();

            foreach (var item in list)
            {
                OrderBagEntity o = new OrderBagEntity();
                o.BagCode = item.BagCode;
                o.BagName = item.BagName;
                o.CompleteTime = item.CompleteTime;
                o.FetchTime = item.FetchTime;
                o.IsEdit = item.EditUserID;
                o.OrderBagID = item.OrderBagId;
                o.OrderTime = item.OrderTime;
                //o.PreCompleteTime = OrderBagDAL.GetPreCompleteTimeByOrderBag(o.OrderBagID);
                o.PreCompleteTime = DateTime.Now;
                o.Priority = item.Priority;
                o.Remark = item.Remark;
                if (!item.ShopReference.IsLoaded)
                    item.ShopReference.Load();
                o.ShopName = item.Shop.shopName;
                OrderBagStatus estatus = (OrderBagStatus)item.Status;
                o.StatusValue = item.Status;
                switch (estatus)
                {
                    case OrderBagStatus.Doing:
                        o.Status = "正常生产"; break;
                    case OrderBagStatus.UnComplete:
                        o.Status = "未完成"; break;
                    case OrderBagStatus.Eroor:
                        o.Status = "发生错误"; break;
                    case OrderBagStatus.Completed:
                        o.Status = "已完成"; break;
                    case OrderBagStatus.Fetched:
                        o.Status = "已结单"; break;
                    case OrderBagStatus.ProductStop:
                        o.Status = "有产品暂停"; break;
                    case OrderBagStatus.OrderBagStop:
                        o.Status = "取消生产"; break;
                    case OrderBagStatus.WaitPhotoMove:
                        o.Status = "移动照片"; break;
                    case OrderBagStatus.Deleted:
                        o.Status = "已删除"; break;
                    case OrderBagStatus.WaitPay:
                        o.Status = "等待付款"; break;
                    default: o.Status = "未知状态"; break;
                }
                int t = item.Type;

                switch (t)
                {
                    case 0:
                        o.Type = "正常"; break;
                    default:
                        o.Type = "返单"; break;
                }
                if (!item.usersReference.IsLoaded)
                    item.usersReference.Load();
                o.UserID = item.users.userID;
                o.UserName = item.users.UserName;
                collection.Add(o);
            }

            _resultList = collection;
            return collection;
        }
        /// <summary>
        /// 得到订单的指定页数据(备份)
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public List<OrderBagEntity> GetRequestPageData_bk(int start, int end)
        {
            List<Tb_OrderBag_BK> list = OrderBagDAL.GetRequestPageData_bk(start, end);
            List<OrderBagEntity> collection = new List<OrderBagEntity>();

            foreach (var item in list)
            {
                OrderBagEntity o = new OrderBagEntity();
                o.BagCode = item.BagCode;
                o.BagName = item.BagName;
                o.CompleteTime = item.CompleteTime;
                o.FetchTime = item.FetchTime;
                o.IsEdit = item.EditUserID;
                o.OrderBagID = item.OrderBagId;
                o.OrderTime = item.OrderTime;
                //o.PreCompleteTime = OrderBagDAL.GetPreCompleteTimeByOrderBag(o.OrderBagID);
                o.PreCompleteTime = DateTime.Now;
                o.Priority = item.Priority;
                o.Remark = item.Remark;
                if (!item.ShopReference.IsLoaded)
                    item.ShopReference.Load();
                o.ShopName = item.Shop.shopName;
                OrderBagStatus estatus = (OrderBagStatus)item.Status;
                o.StatusValue = item.Status;
                switch (estatus)
                {
                    case OrderBagStatus.Doing:
                        o.Status = "正常生产"; break;
                    case OrderBagStatus.UnComplete:
                        o.Status = "未完成"; break;
                    case OrderBagStatus.Eroor:
                        o.Status = "发生错误"; break;
                    case OrderBagStatus.Completed:
                        o.Status = "已完成"; break;
                    case OrderBagStatus.Fetched:
                        o.Status = "已结单"; break;
                    case OrderBagStatus.ProductStop:
                        o.Status = "有产品暂停"; break;
                    case OrderBagStatus.OrderBagStop:
                        o.Status = "取消生产"; break;
                    case OrderBagStatus.WaitPhotoMove:
                        o.Status = "移动照片"; break;
                    case OrderBagStatus.Deleted:
                        o.Status = "已删除"; break;
                    default: o.Status = "未知状态"; break;
                }
                int t = item.Type;

                switch (t)
                {
                    case 0:
                        o.Type = "正常"; break;
                    default:
                        o.Type = "返单"; break;
                }
                if (!item.usersReference.IsLoaded)
                    item.usersReference.Load();
                o.UserID = item.users.userID;
                o.UserName = item.users.UserName;
                collection.Add(o);
            }

            _resultList = collection;
            return collection;
        }

        /// <summary>
        /// 订单数
        /// </summary>
        /// <returns></returns>
        public int GetOrderBagCount(int status)
        {
            try
            {
                return OrderBagDAL.GetRecordCount(status);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 订单数(备份)
        /// </summary>
        /// <returns></returns>
        public int GetOrderBagCount_bk()
        {
            try
            {
                return OrderBagDAL.GetRecordCount_bk();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取在查询状态下，订单总量、产品总量
        /// </summary>
        public void GetTotalOrderNum(out int orderNum, out int productNum)
        {
            orderNum = _resultList.Count;
            productNum = 0;
            foreach (OrderBagEntity order in _resultList)
            {
                productNum += OrderBagDAL.GetProductNumInOrder(order.OrderBagID);
            }
        }
        /// <summary>
        /// 产品数
        /// </summary>
        /// <returns></returns>
        public int GetOrderInfoCount(int orderStatus)
        {
            try
            {
                return OrderBagDAL.GetRecordProductCount(orderStatus);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 产品数(备份)
        /// </summary>
        /// <returns></returns>
        public int GetOrderInfoCount_bk()
        {
            try
            {
                return OrderBagDAL.GetRecordProductCount_bk();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        /// <summary>
        /// 根据订单组合条件得到查询结果
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public List<OrderBagEntity> GetListByComCondition(criteriaOrderBag obj)
        {
            List<Tb_OrderBag> list = OrderBagDAL.GetOrderBagListByComCondtion(obj);
            List<Tb_OrderBag_BK> list_bk = OrderBagDAL.GetOrderBagListByComCondtion_bk(obj);

            if (_resultList.Count > 0)
            {
                _resultList.Clear();
            }
            foreach (var item in list)
            {
                OrderBagEntity o = new OrderBagEntity();
                o.BagCode = item.BagCode;
                o.BagName = item.BagName;
                o.CompleteTime = item.CompleteTime;
                o.FetchTime = item.FetchTime;
                o.IsEdit = item.EditUserID;
                o.OrderBagID = item.OrderBagId;
                o.OrderTime = item.OrderTime;
                //o.PreCompleteTime = OrderBagDAL.GetPreCompleteTimeByOrderBag(o.OrderBagID);
                o.PreCompleteTime = item.FetchTime;
                o.Priority = item.Priority;
                o.Remark = item.Remark;
                if (!item.ShopReference.IsLoaded)
                    item.ShopReference.Load();
                o.ShopName = item.Shop.shopName;
                OrderBagStatus estatus = (OrderBagStatus)item.Status;
                o.StatusValue = item.Status;
                switch (estatus)
                {
                    case OrderBagStatus.Doing:
                        o.Status = "正常生产"; break;
                    case OrderBagStatus.UnComplete:
                        o.Status = "未完成"; break;
                    case OrderBagStatus.Eroor:
                        o.Status = "发生错误"; break;
                    case OrderBagStatus.Completed:
                        o.Status = "已完成"; break;
                    case OrderBagStatus.Fetched:
                        o.Status = "已结单"; break;
                    case OrderBagStatus.ProductStop:
                        o.Status = "有产品暂停"; break;
                    case OrderBagStatus.OrderBagStop:
                        o.Status = "取消生产"; break;
                    case OrderBagStatus.WaitPhotoMove:
                        o.Status = "移动照片"; break;
                    case OrderBagStatus.Deleted:
                        o.Status = "已删除"; break;
                    default: o.Status = "未知状态"; break;
                }
                int t = item.Type;

                switch (t)
                {
                    case 0:
                        o.Type = "正常"; break;
                    default:
                        o.Type = "返单"; break;
                }
                if (!item.usersReference.IsLoaded)
                    item.usersReference.Load();
                o.UserID = item.users.userID;
                o.UserName = item.users.UserName;
                _resultList.Add(o);
            }

            //备份
            foreach (var item in list_bk)
            {
                OrderBagEntity o = new OrderBagEntity();
                o.BagCode = item.BagCode;
                o.BagName = item.BagName;
                o.CompleteTime = item.CompleteTime;
                o.FetchTime = item.FetchTime;
                o.IsEdit = item.EditUserID;
                o.OrderBagID = item.OrderBagId;
                o.OrderTime = item.OrderTime;
                //o.PreCompleteTime = OrderBagDAL.GetPreCompleteTimeByOrderBag(o.OrderBagID);
                o.PreCompleteTime = item.FetchTime;
                o.Priority = item.Priority;
                o.Remark = item.Remark;
                if (!item.ShopReference.IsLoaded)
                    item.ShopReference.Load();
                o.ShopName = item.Shop.shopName;
                OrderBagStatus estatus = (OrderBagStatus)item.Status;
                o.StatusValue = item.Status;
                switch (estatus)
                {
                    case OrderBagStatus.Doing:
                        o.Status = "正常生产"; break;
                    case OrderBagStatus.UnComplete:
                        o.Status = "未完成"; break;
                    case OrderBagStatus.Eroor:
                        o.Status = "发生错误"; break;
                    case OrderBagStatus.Completed:
                        o.Status = "已完成"; break;
                    case OrderBagStatus.Fetched:
                        o.Status = "已结单"; break;
                    case OrderBagStatus.ProductStop:
                        o.Status = "有产品暂停"; break;
                    case OrderBagStatus.OrderBagStop:
                        o.Status = "取消生产"; break;
                    case OrderBagStatus.WaitPhotoMove:
                        o.Status = "移动照片"; break;
                    case OrderBagStatus.Deleted:
                        o.Status = "已删除"; break;
                    default: o.Status = "未知状态"; break;
                }
                int t = item.Type;

                switch (t)
                {
                    case 0:
                        o.Type = "正常"; break;
                    default:
                        o.Type = "返单"; break;
                }
                if (!item.usersReference.IsLoaded)
                    item.usersReference.Load();
                o.UserID = item.users.userID;
                o.UserName = item.users.UserName;
                _resultList.Add(o);
            }
            //记录查询结果
            return _resultList;
        }
        /// <summary>
        /// 根据商户组合条件得到查询结果
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public List<OrderBagEntity> GetListByShopComCondition(criteriaShop obj)
        {
            List<Tb_OrderBag> list = OrderBagDAL.GetOrderBagListByShopComCondition(obj);
            List<Tb_OrderBag_BK> list_bk = OrderBagDAL.GetOrderBagListByShopComCondition_bk(obj);

            if (_resultList.Count > 0)
            {
                _resultList.Clear();
            }


            foreach (var item in list)
            {
                OrderBagEntity o = new OrderBagEntity();
                o.BagCode = item.BagCode;
                o.BagName = item.BagName;
                o.CompleteTime = item.CompleteTime;
                o.FetchTime = item.FetchTime;
                o.IsEdit = item.EditUserID;
                o.OrderBagID = item.OrderBagId;
                o.OrderTime = item.OrderTime;
                o.PreCompleteTime = OrderBagDAL.GetPreCompleteTimeByOrderBag(o.OrderBagID);
                o.Priority = item.Priority;
                o.Remark = item.Remark;
                if (!item.ShopReference.IsLoaded)
                    item.ShopReference.Load();
                o.ShopName = item.Shop.shopName;
                OrderBagStatus estatus = (OrderBagStatus)item.Status;
                o.StatusValue = item.Status;
                switch (estatus)
                {
                    case OrderBagStatus.Doing:
                        o.Status = "正常生产"; break;
                    case OrderBagStatus.UnComplete:
                        o.Status = "未完成"; break;
                    case OrderBagStatus.Eroor:
                        o.Status = "发生错误"; break;
                    case OrderBagStatus.Completed:
                        o.Status = "已完成"; break;
                    case OrderBagStatus.Fetched:
                        o.Status = "已结单"; break;
                    case OrderBagStatus.ProductStop:
                        o.Status = "有产品暂停"; break;
                    case OrderBagStatus.OrderBagStop:
                        o.Status = "取消生产"; break;
                    case OrderBagStatus.WaitPhotoMove:
                        o.Status = "移动照片"; break;
                    case OrderBagStatus.Deleted:
                        o.Status = "已删除"; break;
                    default: o.Status = "未知状态"; break;
                }
                int t = item.Type;

                switch (t)
                {
                    case 0:
                        o.Type = "正常"; break;
                    default:
                        o.Type = "返单"; break;
                }
                if (!item.usersReference.IsLoaded)
                    item.usersReference.Load();
                o.UserID = item.users.userID;
                o.UserName = item.users.UserName;
                _resultList.Add(o);
            }

            //备份
            foreach (var item in list_bk)
            {
                OrderBagEntity o = new OrderBagEntity();
                o.BagCode = item.BagCode;
                o.BagName = item.BagName;
                o.CompleteTime = item.CompleteTime;
                o.FetchTime = item.FetchTime;
                o.IsEdit = item.EditUserID;
                o.OrderBagID = item.OrderBagId;
                o.OrderTime = item.OrderTime;
                o.PreCompleteTime = OrderBagDAL.GetPreCompleteTimeByOrderBag(o.OrderBagID);
                o.Priority = item.Priority;
                o.Remark = item.Remark;
                if (!item.ShopReference.IsLoaded)
                    item.ShopReference.Load();
                o.ShopName = item.Shop.shopName;
                OrderBagStatus estatus = (OrderBagStatus)item.Status;
                o.StatusValue = item.Status;
                switch (estatus)
                {
                    case OrderBagStatus.Doing:
                        o.Status = "正常生产"; break;
                    case OrderBagStatus.UnComplete:
                        o.Status = "未完成"; break;
                    case OrderBagStatus.Eroor:
                        o.Status = "发生错误"; break;
                    case OrderBagStatus.Completed:
                        o.Status = "已完成"; break;
                    case OrderBagStatus.Fetched:
                        o.Status = "已结单"; break;
                    case OrderBagStatus.ProductStop:
                        o.Status = "有产品暂停"; break;
                    case OrderBagStatus.OrderBagStop:
                        o.Status = "取消生产"; break;
                    case OrderBagStatus.WaitPhotoMove:
                        o.Status = "移动照片"; break;
                    case OrderBagStatus.Deleted:
                        o.Status = "已删除"; break;
                    default: o.Status = "未知状态"; break;
                }
                int t = item.Type;

                switch (t)
                {
                    case 0:
                        o.Type = "正常"; break;
                    default:
                        o.Type = "返单"; break;
                }
                if (!item.usersReference.IsLoaded)
                    item.usersReference.Load();
                o.UserID = item.users.userID;
                o.UserName = item.users.UserName;
                _resultList.Add(o);
            }
            //记录查询结果

            return _resultList;
        }

        /// <summary>
        /// 根据产品组合条件查询
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public List<OrderBagEntity> GetListByProductInfoComCondition(criteriaProductInfo obj)
        {
            List<Tb_OrderBag> list = OrderBagDAL.GetOrderBagListByProductComCondition(obj);
            List<Tb_OrderBag_BK> list_bk = OrderBagDAL.GetOrderBagListByProductComCondition_bk(obj);

            if (_resultList.Count > 0)
            {
                _resultList.Clear();
            }


            foreach (var item in list)
            {
                OrderBagEntity o = new OrderBagEntity();
                o.BagCode = item.BagCode;
                o.BagName = item.BagName;
                o.CompleteTime = item.CompleteTime;
                o.FetchTime = item.FetchTime;
                o.IsEdit = item.EditUserID;
                o.OrderBagID = item.OrderBagId;
                o.OrderTime = item.OrderTime;
                o.PreCompleteTime = OrderBagDAL.GetPreCompleteTimeByOrderBag(o.OrderBagID);
                o.Priority = item.Priority;
                o.Remark = item.Remark;
                if (!item.ShopReference.IsLoaded)
                    item.ShopReference.Load();
                o.ShopName = item.Shop.shopName;
                OrderBagStatus estatus = (OrderBagStatus)item.Status;
                o.StatusValue = item.Status;
                switch (estatus)
                {
                    case OrderBagStatus.Doing:
                        o.Status = "正常生产"; break;
                    case OrderBagStatus.UnComplete:
                        o.Status = "未完成"; break;
                    case OrderBagStatus.Eroor:
                        o.Status = "发生错误"; break;
                    case OrderBagStatus.Completed:
                        o.Status = "已完成"; break;
                    case OrderBagStatus.Fetched:
                        o.Status = "已结单"; break;
                    case OrderBagStatus.ProductStop:
                        o.Status = "有产品暂停"; break;
                    case OrderBagStatus.OrderBagStop:
                        o.Status = "取消生产"; break;
                    case OrderBagStatus.WaitPhotoMove:
                        o.Status = "移动照片"; break;
                    case OrderBagStatus.Deleted:
                        o.Status = "已删除"; break;
                    default: o.Status = "未知状态"; break;
                }
                int t = item.Type;

                switch (t)
                {
                    case 0:
                        o.Type = "正常"; break;
                    default:
                        o.Type = "返单"; break;
                }
                if (!item.usersReference.IsLoaded)
                    item.usersReference.Load();
                o.UserID = item.users.userID;
                o.UserName = item.users.UserName;
                _resultList.Add(o);
            }

            //备份
            foreach (var item in list_bk)
            {
                OrderBagEntity o = new OrderBagEntity();
                o.BagCode = item.BagCode;
                o.BagName = item.BagName;
                o.CompleteTime = item.CompleteTime;
                o.FetchTime = item.FetchTime;
                o.IsEdit = item.EditUserID;
                o.OrderBagID = item.OrderBagId;
                o.OrderTime = item.OrderTime;
                o.PreCompleteTime = OrderBagDAL.GetPreCompleteTimeByOrderBag_bk(o.OrderBagID);
                o.Priority = item.Priority;
                o.Remark = item.Remark;
                if (!item.ShopReference.IsLoaded)
                    item.ShopReference.Load();
                o.ShopName = item.Shop.shopName;
                OrderBagStatus estatus = (OrderBagStatus)item.Status;
                o.StatusValue = item.Status;
                switch (estatus)
                {
                    case OrderBagStatus.Doing:
                        o.Status = "正常生产"; break;
                    case OrderBagStatus.UnComplete:
                        o.Status = "未完成"; break;
                    case OrderBagStatus.Eroor:
                        o.Status = "发生错误"; break;
                    case OrderBagStatus.Completed:
                        o.Status = "已完成"; break;
                    case OrderBagStatus.Fetched:
                        o.Status = "已结单"; break;
                    case OrderBagStatus.ProductStop:
                        o.Status = "有产品暂停"; break;
                    case OrderBagStatus.OrderBagStop:
                        o.Status = "取消生产"; break;
                    case OrderBagStatus.WaitPhotoMove:
                        o.Status = "移动照片"; break;
                    case OrderBagStatus.Deleted:
                        o.Status = "已删除"; break;
                    default: o.Status = "未知状态"; break;
                }
                int t = item.Type;

                switch (t)
                {
                    case 0:
                        o.Type = "正常"; break;
                    default:
                        o.Type = "返单"; break;
                }
                if (!item.usersReference.IsLoaded)
                    item.usersReference.Load();
                o.UserID = item.users.userID;
                o.UserName = item.users.UserName;
                _resultList.Add(o);
            }
            //记录查询结果

            return _resultList;
        }

        /// <summary>
        /// 根据操作的员工查询
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<OrderBagEntity> GetListByUserInfoComCondition(UserSearchInfo condition)
        {
            List<Tb_OrderBag> list = OrderBagDAL.GetOrderBagListByUserInfo(condition);
            List<Tb_OrderBag_BK> list_bk = OrderBagDAL.GetOrderBagListByUserInfo_bk(condition);
            List<OrderBagEntity> colletion = new List<OrderBagEntity>();


            foreach (var item in list)
            {
                OrderBagEntity o = new OrderBagEntity();
                o.BagCode = item.BagCode;
                o.BagName = item.BagName;
                o.CompleteTime = item.CompleteTime;
                o.FetchTime = item.FetchTime;
                o.IsEdit = item.EditUserID;
                o.OrderBagID = item.OrderBagId;
                o.OrderTime = item.OrderTime;
                o.PreCompleteTime = OrderBagDAL.GetPreCompleteTimeByOrderBag(o.OrderBagID);
                o.Priority = item.Priority;
                o.Remark = item.Remark;
                if (!item.ShopReference.IsLoaded)
                    item.ShopReference.Load();
                o.ShopName = item.Shop.shopName;
                OrderBagStatus estatus = (OrderBagStatus)item.Status;
                o.StatusValue = item.Status;
                switch (estatus)
                {
                    case OrderBagStatus.Doing:
                        o.Status = "正常生产"; break;
                    case OrderBagStatus.UnComplete:
                        o.Status = "未完成"; break;
                    case OrderBagStatus.Eroor:
                        o.Status = "发生错误"; break;
                    case OrderBagStatus.Completed:
                        o.Status = "已完成"; break;
                    case OrderBagStatus.Fetched:
                        o.Status = "已结单"; break;
                    case OrderBagStatus.ProductStop:
                        o.Status = "有产品暂停"; break;
                    case OrderBagStatus.OrderBagStop:
                        o.Status = "取消生产"; break;
                    case OrderBagStatus.WaitPhotoMove:
                        o.Status = "移动照片"; break;
                    case OrderBagStatus.Deleted:
                        o.Status = "已删除"; break;
                    default: o.Status = "未知状态"; break;
                }
                int t = item.Type;

                switch (t)
                {
                    case 0:
                        o.Type = "正常"; break;
                    default:
                        o.Type = "返单"; break;
                }
                if (!item.usersReference.IsLoaded)
                    item.usersReference.Load();
                o.UserID = item.users.userID;
                o.UserName = item.users.UserName;
                colletion.Add(o);
            }

            //备份
            foreach (var item in list_bk)
            {
                OrderBagEntity o = new OrderBagEntity();
                o.BagCode = item.BagCode;
                o.BagName = item.BagName;
                o.CompleteTime = item.CompleteTime;
                o.FetchTime = item.FetchTime;
                o.IsEdit = item.EditUserID;
                o.OrderBagID = item.OrderBagId;
                o.OrderTime = item.OrderTime;
                //o.PreCompleteTime = OrderBagDAL.GetPreCompleteTimeByOrderBag(o.OrderBagID);
                o.Priority = item.Priority;
                o.Remark = item.Remark;
                if (!item.ShopReference.IsLoaded)
                    item.ShopReference.Load();
                o.ShopName = item.Shop.shopName;
                OrderBagStatus estatus = (OrderBagStatus)item.Status;
                o.StatusValue = item.Status;
                switch (estatus)
                {
                    case OrderBagStatus.Doing:
                        o.Status = "正常生产"; break;
                    case OrderBagStatus.UnComplete:
                        o.Status = "未完成"; break;
                    case OrderBagStatus.Eroor:
                        o.Status = "发生错误"; break;
                    case OrderBagStatus.Completed:
                        o.Status = "已完成"; break;
                    case OrderBagStatus.Fetched:
                        o.Status = "已结单"; break;
                    case OrderBagStatus.ProductStop:
                        o.Status = "有产品暂停"; break;
                    case OrderBagStatus.OrderBagStop:
                        o.Status = "取消生产"; break;
                    case OrderBagStatus.WaitPhotoMove:
                        o.Status = "移动照片"; break;
                    case OrderBagStatus.Deleted:
                        o.Status = "已删除"; break;
                    default: o.Status = "未知状态"; break;
                }
                int t = item.Type;

                switch (t)
                {
                    case 0:
                        o.Type = "正常"; break;
                    default:
                        o.Type = "返单"; break;
                }
                if (!item.usersReference.IsLoaded)
                    item.usersReference.Load();
                o.UserID = item.users.userID;
                o.UserName = item.users.UserName;
                colletion.Add(o);
            }
            //记录查询结果
            _resultList = colletion;
            return colletion;
        }

        /// <summary>
        /// 得到所有的产品类别
        /// </summary>
        /// <returns></returns>
        public List<ProductCate> GetAllProductCate()
        {
            return OrderBagDAL.GetAllProductCate();
        }

        /// <summary>
        /// 得到产品类别的所有产品
        /// </summary>
        /// <param name="cateid"></param>
        /// <returns></returns>
        public List<Product> GetProductByCate(int cateid)
        {
            return OrderBagDAL.GetProductByCate(cateid);
        }

        /// <summary>
        /// 得到所有的商品信息
        /// </summary>
        /// <returns></returns>
        public List<ShopInfo> GetAllShopInfo()
        {
            return ShopDAL.GetAllShopInfo();
        }

        /// <summary>
        /// 得到GRID中的所有bagCode
        /// </summary>
        /// <returns></returns>
        public List<string> GetShowOrderCode()
        {
            List<string> codeList = new List<string>();

            foreach (OrderBagEntity item in _resultList)
            {
                codeList.Add(item.BagCode);
            }

            return codeList;
        }
        /// <summary>
        /// 获取GRID中所有的BagName
        /// </summary>
        /// <returns></returns>
        public List<string> GetShowOrderName()
        {
            List<string> nameList = new List<string>();
            foreach (OrderBagEntity item in _resultList)
            {
                nameList.Add(item.BagName);
            }
            return nameList;
        }

        /// <summary>
        /// 得到GRID中的所有priority
        /// </summary>
        /// <returns></returns>
        public List<int> GetShowPriority()
        {
            List<int> priorityList = new List<int>();

            foreach (OrderBagEntity item in _resultList)
            {
                if (!priorityList.Contains(item.Priority))
                {
                    priorityList.Add(item.Priority);
                }
            }

            return priorityList;
        }

        /// <summary>
        /// 得到所有查询到的订单的产品数量的和
        /// </summary>
        /// <returns></returns>
        public int GetAllResultProductCount(List<OrderBagEntity> list)
        {
            int count = 0;

            foreach (var item in list)
            {
                count += OrderInfoDAL.GetProductCountByBagID(item.OrderBagID);
            }

            return count;
        }

        #endregion


        #region //快速查询

        //#region//对查询的结果进行快速过滤加上分页功能

        ///// <summary>
        ///// 对查询的结果进行分页过滤处理
        ///// </summary>
        ///// <param name="condition"></param>
        ///// <param name="startIndex"></param>
        ///// <param name="endIndex"></param>
        ///// <returns></returns>
        //public List<OrderBagEntity> RapidSearchByResult(RapidCondition condition, int startIndex, int endIndex)
        //{
        //    List<OrderBagEntity> collection = new List<OrderBagEntity>();

        //}

        //#endregion

        /// <summary>
        /// 在结果集中快速筛选
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public List<OrderBagEntity> RapidSearch(RapidCondition condition)
        {
            List<OrderBagEntity> collection = new List<OrderBagEntity>();
            bool IsSearched = false;
            //如果输入订单名称
            if (string.Compare(condition.bagName, string.Empty) != 0)
            {
                IsSearched = true;
                foreach (OrderBagEntity item in _resultList)
                {
                    if (item.BagName.Contains(condition.bagName))
                    {
                        collection.Add(item);
                    }
                }
            }
            //根据订单CODE匹配
            if (string.Compare(condition.bagCode, string.Empty) != 0)
            {
                if (IsSearched)
                {
                    List<OrderBagEntity> deleteList = new List<OrderBagEntity>();

                    foreach (OrderBagEntity item in collection)
                    {
                        if (!item.BagCode.Contains(condition.bagCode))
                        {
                            deleteList.Add(item);
                        }
                    }

                    //过滤掉不符合条件的记录
                    foreach (OrderBagEntity item in deleteList)
                    {
                        collection.Remove(item);
                    }
                }
                else
                {
                    foreach (OrderBagEntity item in _resultList)
                    {
                        if (item.BagCode.Contains(condition.bagCode))
                        {
                            collection.Add(item);
                        }
                    }
                    IsSearched = true;
                }
            }
            //根据加急匹配
            if (condition.priority != -1)
            {
                List<OrderBagEntity> deleteList = new List<OrderBagEntity>();
                if (IsSearched)
                {
                    foreach (OrderBagEntity item in collection)
                    {
                        if (item.Priority != condition.priority)
                        {
                            deleteList.Add(item);
                        }
                    }
                    //过滤不合条件的记录
                    foreach (OrderBagEntity item in deleteList)
                    {
                        collection.Remove(item);
                    }
                }
                else
                {
                    foreach (OrderBagEntity item in _resultList)
                    {
                        if (item.Priority == condition.priority)
                        {
                            collection.Add(item);
                        }
                    }
                    IsSearched = true;
                }
            }
            //过滤状态
            /*
            if (condition.status != -1)
            {
                List<OrderBagEntity> deleteList = new List<OrderBagEntity>();
                if (IsSearched)
                {
                    foreach (OrderBagEntity item in collection)
                    {
                        if (item.Obj.Status != condition.status-1)
                        {
                            deleteList.Add(item);
                        }
                    }

                    foreach (OrderBagEntity item in deleteList)
                    {
                        collection.Remove(item);
                    }
                }
                else
                {
                    foreach (OrderBagEntity item in _resultList)
                    {
                        if (item.Obj.Status == condition.status-1)
                        {
                            collection.Add(item);
                        }
                    }
                    IsSearched = true;
                }
            }
             * */
            //下单开始时间
            if (condition.orderStart != DateTime.MinValue)
            {
                if (IsSearched)
                {
                    List<OrderBagEntity> deleteList = new List<OrderBagEntity>();
                    foreach (OrderBagEntity item in collection)
                    {
                        if (item.OrderTime < condition.orderStart)
                        {
                            deleteList.Add(item);
                        }
                    }
                    //过滤不合条件的记录
                    foreach (OrderBagEntity item in deleteList)
                    {
                        collection.Remove(item);
                    }
                }
                else
                {
                    foreach (OrderBagEntity item in _resultList)
                    {
                        if (item.OrderTime > condition.orderStart)
                        {
                            collection.Add(item);
                        }
                    }

                    IsSearched = true;
                }
            }
            //下单截止时间
            if (condition.orderEnd != DateTime.MinValue)
            {
                List<OrderBagEntity> deleteList = new List<OrderBagEntity>();
                if (IsSearched)
                {
                    foreach (OrderBagEntity item in collection)
                    {
                        if (item.OrderTime > condition.orderEnd)
                        {
                            deleteList.Add(item);
                        }
                    }

                    foreach (OrderBagEntity item in deleteList)
                    {
                        collection.Remove(item);
                    }
                }
                else
                {
                    foreach (OrderBagEntity item in _resultList)
                    {
                        if (item.OrderTime < condition.orderEnd)
                        {
                            collection.Add(item);
                        }
                    }
                    IsSearched = true;
                }
            }
            //取单开始时间
            if (condition.fetchStart != DateTime.MinValue)
            {
                List<OrderBagEntity> deleteList = new List<OrderBagEntity>();
                if (IsSearched)
                {
                    foreach (OrderBagEntity item in collection)
                    {
                        if (item.FetchTime < condition.fetchStart)
                        {
                            deleteList.Add(item);
                        }
                    }

                    foreach (OrderBagEntity item in deleteList)
                    {
                        collection.Remove(item);
                    }
                }
                else
                {
                    foreach (OrderBagEntity item in _resultList)
                    {
                        if (item.FetchTime > condition.fetchStart)
                        {
                            collection.Add(item);
                        }
                    }
                    IsSearched = true;
                }
            }
            //取单结束时间
            if (condition.fetchEnd != DateTime.MinValue)
            {
                List<OrderBagEntity> deleteList = new List<OrderBagEntity>();
                if (IsSearched)
                {
                    foreach (OrderBagEntity item in collection)
                    {
                        if (item.FetchTime > condition.fetchEnd)
                        {
                            deleteList.Add(item);
                        }
                    }

                    foreach (OrderBagEntity item in deleteList)
                    {
                        collection.Remove(item);
                    }
                }
                else
                {
                    foreach (OrderBagEntity item in _resultList)
                    {
                        if (item.FetchTime < condition.fetchEnd)
                        {
                            collection.Add(item);
                        }
                    }
                    IsSearched = true;
                }
            }
            //最新修改
            //根据流程过滤
            if (condition.flowList != -1)
            {
                if (IsSearched)
                {
                    List<OrderBagEntity> deleteList = new List<OrderBagEntity>();

                    foreach (OrderBagEntity item in collection)
                    {
                        //判断订单中是否有正在生产的并且是当前流程状态的
                        if (!OrderInfoDAL.IsHaveFlowStatus(condition.flowList, item.OrderBagID))
                        {
                            deleteList.Add(item);
                        }
                    }

                    foreach (var item in deleteList)
                    {
                        collection.Remove(item);
                    }
                }
                else
                {
                    foreach (var item in _resultList)
                    {
                        if (OrderInfoDAL.IsHaveFlowStatus(condition.flowList, item.OrderBagID))
                        {
                            collection.Add(item);
                        }
                    }

                    IsSearched = true;
                }

            }

            //当前操作的员工
            if (condition.userID != -1)
            {
                if (IsSearched)
                {
                    List<OrderBagEntity> deleteList = new List<OrderBagEntity>();

                    foreach (var item in collection)
                    {
                        if (!OrderInfoDAL.IsUserProductingOrderBag(condition.userID, item.OrderBagID))
                        {
                            deleteList.Add(item);
                        }
                    }

                    foreach (var item in deleteList)
                    {
                        collection.Remove(item);
                    }
                }
                else
                {
                    foreach (var item in _resultList)
                    {
                        if (OrderInfoDAL.IsUserProductingOrderBag(condition.userID, item.OrderBagID))
                        {
                            collection.Add(item);
                        }
                    }
                    IsSearched = true;
                }
            }


            //如果没有输入查询条件
            if (string.Compare(condition.bagName, string.Empty) == 0 &&
                string.Compare(condition.bagCode, string.Empty) == 0 &&
                condition.priority == -1 &&
                condition.orderStart == DateTime.MinValue &&
                condition.orderEnd == DateTime.MinValue &&
                condition.fetchStart == DateTime.MinValue &&
                condition.fetchEnd == DateTime.MinValue &&
                //condition.status == -1 &&
                condition.flowList == -1 &&
                condition.userID == -1)
            {
                return _resultList;
            }


            return collection;
        }

        #endregion

        #region //update methods


        #endregion

        #endregion

        //-----------------------------------------------后期修改所作的操作--------------------------------------------------------
        /// <summary>
        /// 获取所有的流程节点，并缓存起来
        /// </summary>
        public void SetAllTechs()
        {
            _resultTechList = WorkerOrderFlowListDAL.GetAllTechName();
        }
        /// <summary>
        /// 获取当前显示列表内当前流程的集合
        /// </summary>
        /// <returns></returns>
        public List<TechInfo> GetCurrentTechs()
        {
            try
            {
                List<TechInfo> ListTech = new List<TechInfo>();
                TechInfo ti = new TechInfo();
                ti.TechID = -1;
                ti.TechName = "";
                ListTech.Add(ti);
                foreach (OrderBagEntity o in _resultList)
                {
                    List<orderInfo> ListOrderInfo = OrderInfoDAL.GetOrderInfoByBag(o.OrderBagID);
                    foreach (orderInfo p in ListOrderInfo)
                    {
                        int r = OrderInfoDAL.GetCurrentTechInOrderInfo(p.OrderProductId);
                        if (r != -1)
                        {
                            foreach (TechInfo t in _resultTechList)
                            {
                                if (t.TechID == r)
                                {
                                    if (!ListTech.Contains(t))
                                    {
                                        ListTech.Add(t);
                                    }
                                }
                            }
                        }
                    }
                }
                return ListTech;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取当前订单集合的加急级别
        /// </summary>
        /// <returns></returns>
        public List<int> GetCurrentPriority()
        {
            try
            {
                List<int> ListPriority = new List<int>();
                foreach (OrderBagEntity p in _resultList)
                {
                    if (!ListPriority.Contains(p.Priority))
                    {
                        ListPriority.Add(p.Priority);
                    }
                }
                return ListPriority;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 更新指定订单的加急级别
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="priority"></param>
        public void UpdatePriority(int orderId, int priority)
        {
            try
            {
                OrderBagDAL.UpdatePriority(orderId, priority);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 更新订单与其所有的产品状态
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="status"></param>
        public void UpdateOrderAndOrderInfoStatus(int orderId, int orderStatus, int productStatus)
        {
            try
            {
                OrderBagDAL.UpdateOrderAndOrderInfoStatus(orderId, orderStatus, productStatus);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改产品的状态(停止与恢复)
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="productStatus"></param>
        public void UpdateProductStatus(int productId, int productStatus)
        {
            try
            {
                OrderBagDAL.UpdateProductStatus(productId, productStatus);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 单独修改订单的状态(停止与恢复)
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="orderStatus"></param>
        public void UpdateOrderStatus(int orderId, int orderStatus)
        {
            try
            {
                OrderBagDAL.UpdataOrderStatus(orderId, orderStatus);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 插入停止还是恢复产品的备注(以订单为单位)
        /// </summary>
        /// <param name="status"></param>
        public void InsertStopOrRecorverRemark(int orderId, int status, string remark, int userId)
        {
            try
            {
                OrderBagDAL.InsertStopOrRecorverRemark(orderId, status, remark, userId);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 插入停止还是恢复产品的备注(以产品为单位)
        /// </summary>
        /// <param name="status"></param>
        public void InsertStopOrRecorverRemark2(int productId, int status, string remark, int userId)
        {
            try
            {
                OrderBagDAL.InsertStopOrRecorverRemark3(productId, status, remark, userId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取指定产品的状态
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public int GetOrderInfoStatus(int productId)
        {
            int ret;
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    orderInfo oi = db.orderInfo.FirstOrDefault(o=>o.OrderProductId ==productId);
                    ret = oi.status;

                }
                return ret;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool MakeBigBoardOK(int orderproductId)
        {
            bool ret = false;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                orderInfo oi = db.orderInfo.FirstOrDefault(o=>o.OrderProductId ==orderproductId);
                if (oi != null)
                {
                    if (!oi.photoInfo.IsLoaded)
                    {
                        oi.photoInfo.Load();
                    }
                    foreach (var item in oi.photoInfo)
                    {
                        if (item.status == (int)PhotoStatus.PingBan || item.status == (int)PhotoStatus.PingBanInd)
                        {
                            ret = true;
                            break;
                        }
                    }
                }
            }
            return ret;
        }

        /// <summary>
        /// 删除垃圾订单
        /// </summary>
        /// <param name="userId"></param>
        public void DeleteTempOrder(int userId)
        {
            try
            {
                OrderBagDAL.DeleteTempOrder(userId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取产品的当前节点
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public string GetProductTech(int productId)
        {
            try
            {
                return WorkerOrderFlowListDAL.GetFlowStatusNameByOrderProduct(productId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取产品的基本信息
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public MODEL.CustomerManage.CurrentStyleProduct GetStyleProduct(int productId)
        {
            try
            {
                orderInfo info = OrderBagDAL.GetStyleProduct(productId);
                MODEL.CustomerManage.CurrentStyleProduct product = new MINERP.MODEL.CustomerManage.CurrentStyleProduct();
                if (!info.Tb_ProductReference.IsLoaded)
                    info.Tb_ProductReference.Load();
                if (!info.Tb_Product.tb_ProductTypeReference.IsLoaded)
                    info.Tb_Product.tb_ProductTypeReference.Load();
                product.ProductTypeCode = info.Tb_Product.tb_ProductType.ProductTypeCode;
                product.ProductHeight = info.Height;
                product.ProductWidth = info.Width;
                product.MaxHeight = product.ProductHeight * 1.1;
                product.MinHeight = product.ProductHeight * 0.9;
                product.MaxWidth = product.ProductWidth * 2 * 1.1;
                product.MinWidth = product.ProductWidth * 2 * 0.9;
                return product;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取产品的基本信息(备份)
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public MODEL.CustomerManage.CurrentStyleProduct GetStyleProduct_bk(int productId)
        {
            try
            {
                orderInfo_BK info = OrderBagDAL.GetStyleProduct_bk(productId);
                MODEL.CustomerManage.CurrentStyleProduct product = new MINERP.MODEL.CustomerManage.CurrentStyleProduct();
                if (!info.Tb_ProductReference.IsLoaded)
                    info.Tb_ProductReference.Load();
                if (!info.Tb_Product.tb_ProductTypeReference.IsLoaded)
                    info.Tb_Product.tb_ProductTypeReference.Load();
                product.ProductTypeCode = info.Tb_Product.tb_ProductType.ProductTypeCode;
                product.ProductHeight = info.Height;
                product.ProductWidth = info.Width;
                product.MaxHeight = product.ProductHeight * 1.1;
                product.MinHeight = product.ProductHeight * 0.9;
                product.MaxWidth = product.ProductWidth * 2 * 1.1;
                product.MinWidth = product.ProductWidth * 2 * 0.9;
                return product;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region 删除订单(将订单状态置为删除，不是真删)
        /// <summary>
        /// 删除订单(将订单状态置为删除，不是真删)
        /// </summary>
        /// <param name="orderBagId"></param>
        public void DeleteOrderBag(int orderBagId,int userId)
        {
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    int delStatus = (int)COM.OrderBagStatus.Deleted;
                    int delPhoto = (int)COM.PhotoStatus.OrderDeleted;
                    int delOrderInfo = (int)COM.OrderInfoStatus.Deleted;
                    //Tb_OrderBag order = OrderBagDAL.GetOrderByOrderId(orderBagId);
                    users editUser = db.users.FirstOrDefault(u=>u.userID ==userId);
                    Tb_OrderBag order = db.Tb_OrderBag.FirstOrDefault(p => p.OrderBagId == orderBagId);
                    if (!order.orderInfo.IsLoaded)
                    {
                        order.orderInfo.Load(System.Data.Objects.MergeOption.OverwriteChanges);
                    }
                    if (!order.ShopReference.IsLoaded)
                    {
                        order.ShopReference.Load();
                    }
                    List<orderInfo> infos = order.orderInfo.ToList();
                    List<photoInfo> photos = new List<photoInfo>();

                    #region//删除相关的开单面积
                    CreateOrderAreaByDay cd = db.CreateOrderAreaByDay.FirstOrDefault(c => c.ShopName == order.Shop.shopName && c.CreateOrderDate == order.OrderTime.Date);
                    if (cd != null)
                    {
                        cd.Area -= infos.Sum(o => o.Area);
                        if (cd.Area < 0)
                        {
                            cd.Area = 0;
                        }
                    }
                    #endregion

                    foreach (orderInfo info in infos)
                    {
                        if (!info.photoInfo.IsLoaded)
                        {
                            info.photoInfo.Load(System.Data.Objects.MergeOption.OverwriteChanges);
                        }
                        //List<New_EmployeePerformance> orderEP = OrderBagDAL.GetNEPByOrderId(info.OrderProductId);
                        List<New_EmployeePerformance> orderEP = db.New_EmployeePerformance.Where(o=>o.OrderproductId ==info.OrderProductId).ToList();
                        orderEP.ForEach(p => p.Status = delOrderInfo);//将业绩状态该为删除

                        info.status = delOrderInfo;
                        info.Remark = editUser == null ? "" : editUser.UserName + " 删除时间：" + DateTime.Now + " " + info.Remark;
                        order.Shop.OrderBalance -= info.PrePriceCount == null ? 0 : info.PrePriceCount;
                        if (order.Shop.OrderBalance < 0)
                        {
                            order.Shop.OrderBalance = 0;
                        }
                        photos.AddRange(info.photoInfo.ToList());

                        /*
                        ///吧相关的照片退版
                        info.photoInfo.Load();
                        List<photoInfo> photoList = info.photoInfo.ToList();

                        foreach (photoInfo photo in photoList)
                        {
                            if (photo.status == (int)COM.PhotoStatus.PingBan)
                            {
                                //photo.status = (int)PhotoStatus.DistributedPrinter;
                                photo.picMakeUp.Load();
                                picMakeUp picMake = photo.picMakeUp.OrderByDescending(p => p.ID).FirstOrDefault();
                                if (picMake == null)
                                {
                                    continue;
                                }
                                picMake.BoardReference.Load();
                                picMake.Board.picMakeUp.Load();
                                List<picMakeUp> picMakeList = picMake.Board.picMakeUp.ToList(); ;

                                foreach (picMakeUp picMakeother in picMakeList)
                                {
                                    picMakeother.photoInfoReference.Load();
                                    photoInfo photoPic = picMakeother.photoInfo;

                                    photoPic.status = (int)PhotoStatus.NoDistributePrinter;

                                }


                                Board deBoard = picMake.Board;
                                if (deBoard != null)
                                {
                                    OrderBagDAL.DeleteObject(deBoard);
                                }


                            }
                         *
                         } */
                    }

                    order.Status = delStatus;
                    foreach (photoInfo photo in photos)
                    {
                        photo.status = delPhoto;
                    }
                    if (infos.Count == 0)
                    {
                        db.DeleteObject(infos);
                    }
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception exx)
                    {
                        //OrderBagDAL.RefreshObject(order);
                        throw exx;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteOrderBag(int orderBagId, int orderProductid,int editUserId)
        {
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    int delStatus = (int)COM.OrderBagStatus.Deleted;
                    int delPhoto = (int)COM.PhotoStatus.OrderDeleted;
                    int delOrderInfo = (int)COM.OrderInfoStatus.Deleted;
                    //Tb_OrderBag order = OrderBagDAL.GetOrderByOrderId(orderBagId);
                    //if (!order.orderInfo.IsLoaded)
                    //{
                    //    order.orderInfo.Load(System.Data.Objects.MergeOption.OverwriteChanges);
                    //}
                    //if (!order.ShopReference.IsLoaded)
                    //{
                    //    order.ShopReference.Load();
                    //}
                    //orderInfo info = OrderBagDAL.GetStyleProduct(orderProductid);
                    users editUser = db.users.FirstOrDefault(u=>u.userID ==editUserId);
                    orderInfo info = db.orderInfo.FirstOrDefault(o=>o.OrderProductId==orderProductid);
                    info.Tb_OrderBagReference.Load();
                    Tb_OrderBag order = info.Tb_OrderBag;
                    if (!order.orderInfo.IsLoaded)
                    {
                        order.orderInfo.Load(System.Data.Objects.MergeOption.OverwriteChanges);
                    }
                    if (!order.ShopReference.IsLoaded)
                    {
                        order.ShopReference.Load();
                    }
                    // orderInfo info = order.orderInfo.FirstOrDefault(p => p.OrderProductId == orderProductid);

                    //List<New_EmployeePerformance> orderEP = OrderBagDAL.GetNEPByOrderId(info.OrderProductId);
                    List<New_EmployeePerformance> orderEP = db.New_EmployeePerformance.Where(o => o.OrderproductId == info.OrderProductId).ToList();
                    orderEP.ForEach(p => p.Status = delOrderInfo);//将业绩状态该为删除

                    #region//删除相关的开单面积
                    CreateOrderAreaByDay cd = db.CreateOrderAreaByDay.FirstOrDefault(c => c.ShopName == order.Shop.shopName && c.CreateOrderDate == order.OrderTime.Date);
                    if (cd != null)
                    {
                        cd.Area -= info.Area;
                        if (cd.Area < 0)
                        {
                            cd.Area = 0;
                        }
                    }
                    #endregion

                    List<photoInfo> photos = new List<photoInfo>();


                    if (!info.photoInfo.IsLoaded)
                    {
                        info.photoInfo.Load(System.Data.Objects.MergeOption.OverwriteChanges);
                    }
                    info.status = delOrderInfo;
                    info.Remark = editUser == null ? "" : editUser.UserName + " 删除时间：" + DateTime.Now + " " + info.Remark;
                    order.Shop.OrderBalance -= info.PrePriceCount == null ? 0 : info.PrePriceCount;
                    if (order.Shop.OrderBalance < 0)
                    {
                        order.Shop.OrderBalance = 0;
                    }
                    photos.AddRange(info.photoInfo.ToList());

                    /*
                    ///吧相关的照片退版
                    info.photoInfo.Load();
                    List<photoInfo> photoList = info.photoInfo.ToList();

                    foreach (photoInfo photo in photoList)
                    {
                        if (photo.status == (int)COM.PhotoStatus.PingBan)
                        {
                            //photo.status = (int)PhotoStatus.DistributedPrinter;
                            photo.picMakeUp.Load();
                            picMakeUp picMake = photo.picMakeUp.OrderByDescending(p => p.ID).FirstOrDefault();
                            if (picMake == null)
                            {
                                continue;
                            }
                            picMake.BoardReference.Load();
                            picMake.Board.picMakeUp.Load();
                            List<picMakeUp> picMakeList = picMake.Board.picMakeUp.ToList(); ;

                            foreach (picMakeUp picMakeother in picMakeList)
                            {
                                picMakeother.photoInfoReference.Load();
                                photoInfo photoPic = picMakeother.photoInfo;

                                photoPic.status = (int)PhotoStatus.NoDistributePrinter;

                            }


                            Board deBoard = picMake.Board;
                            if (deBoard != null)
                            {
                                OrderBagDAL.DeleteObject(deBoard);
                            }


                        }
                    }
                     */


                    foreach (photoInfo photo in photos)
                    {
                        photo.status = delPhoto;
                    }
                    if (order.orderInfo.Count == 1)//如果订单中只有一个产品的话将订单一起做删除标记
                    {
                        order.Status = delStatus;
                    }
                    try
                    {
                        //OrderBagDAL.Save();
                        db.SaveChanges();
                    }
                    catch (Exception exx)
                    {
                        //OrderBagDAL.RefreshObject(order);
                        throw exx;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        public int GetProductNum(int style)
        {
            string selectSql = "";
            int RtnInt = 0;
            DateTime stTime;
            DateTime endTime;
            switch (style)
            {
                case (int)MINERP.BLL.CustomerManage.OrderInfoBLL.OrderQueryStyle.DelyOne:
                    stTime = DateTime.Now.AddDays(-1).Date;
                    endTime = DateTime.Now.Date;
                    selectSql = "select count(*) from  orderinfo,Tb_OrderBag where Tb_OrderBag.OrderBagId=orderinfo.OrderBagId and orderinfo.status=" + (int)OrderInfoStatus.Doing + " and OrderTime <'" + endTime + "' and ordertime>'" + stTime+"'";
                    RtnInt = OrderBagDAL.GetProductNum(selectSql);
                    break;
                case (int)MINERP.BLL.CustomerManage.OrderInfoBLL.OrderQueryStyle.DelyTwo:
                    stTime = DateTime.Now.AddDays(-2).Date;
                    endTime = DateTime.Now.AddDays(-1).Date;
                    selectSql = "select count(*) from  orderinfo,Tb_OrderBag where Tb_OrderBag.OrderBagId=orderinfo.OrderBagId and orderinfo.status=" + (int)OrderInfoStatus.Doing + " and OrderTime <'" + endTime + "' and ordertime>'" + stTime+"'";
                    RtnInt = OrderBagDAL.GetProductNum(selectSql);
                    break;
                case (int)MINERP.BLL.CustomerManage.OrderInfoBLL.OrderQueryStyle.DelyMore:
                    stTime = DateTime.Now.AddDays(-3).Date;
                    endTime = DateTime.Now.AddDays(-1).Date;
                    selectSql = "select count(*) from  orderinfo,Tb_OrderBag where Tb_OrderBag.OrderBagId=orderinfo.OrderBagId and orderinfo.status=" + (int)OrderInfoStatus.Doing + " and OrderTime <'" + endTime+"'";
                    RtnInt = OrderBagDAL.GetProductNum(selectSql);
                    break;
                case (int)MINERP.BLL.CustomerManage.OrderInfoBLL.OrderQueryStyle.Stop:
                    //stTime = DateTime.Now.AddDays(-3).Date;
                    //endTime = DateTime.Now.AddDays(-1).Date;
                    selectSql = "select count(*) from  orderinfo,Tb_OrderBag where Tb_OrderBag.OrderBagId=orderinfo.OrderBagId and orderinfo.status=" + (int)OrderInfoStatus.Stop;
                    RtnInt = OrderBagDAL.GetProductNum(selectSql);
                    break;
                case (int)MINERP.BLL.CustomerManage.OrderInfoBLL.OrderQueryStyle.ShenHe:
                    //stTime = DateTime.Now.AddDays(-3).Date;
                    //endTime = DateTime.Now.AddDays(-1).Date;
                    selectSql = "select count(*) from  orderinfo,Tb_OrderBag where Tb_OrderBag.OrderBagId=orderinfo.OrderBagId and (orderinfo.status=" + (int)OrderInfoStatus.WaitCheck + " or orderinfo.status=" + (int)OrderInfoStatus.WaitPay + ")";
                    RtnInt = OrderBagDAL.GetProductNum(selectSql);
                    break;
                case (int)MINERP.BLL.CustomerManage.OrderInfoBLL.OrderQueryStyle.JiaJi:
                    selectSql = "select count(*) from  orderinfo,Tb_OrderBag where Tb_OrderBag.OrderBagId=orderinfo.OrderBagId and orderinfo.status=" + (int)OrderInfoStatus.Doing + " and orderinfo.Priority>=10";
                    RtnInt = OrderBagDAL.GetProductNum(selectSql);
                    break;
            }
            return RtnInt;

        }



        /// <summary>
        /// 将欠款的产品改为正常 生产
        /// </summary>
        /// <param name="_focusOrderInfo"></param>
        /// <param name="_focusOrderBag"></param>
        public void SetOrderToProduct(OrderInfoEntity _focusOrderInfo, OrderBagEntity _focusOrderBag)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Tb_OrderBag obg = db.Tb_OrderBag.FirstOrDefault(ob => ob.OrderBagId == _focusOrderBag.OrderBagID);
                if (obg != null)
                {
                    if (!obg.ShopReference.IsLoaded)
                    {
                        obg.ShopReference.Load();
                    }
                    orderInfo oi = db.orderInfo.FirstOrDefault
                        (o => o.OrderProductId == _focusOrderInfo.OrderProductId);
                    New_EmployeePerformance perFormance = db.New_EmployeePerformance.FirstOrDefault(e => e.OrderproductId == _focusOrderInfo.OrderProductId);
                    if (perFormance != null)
                    {
                        perFormance.Status = (int)OrderInfoStatus.Doing;
                    }
                    if (oi != null)
                    {
                        obg.Shop.OrderBalance += oi.PrePriceCount;
                        oi.status = (int)OrderInfoStatus.Doing;
                    }

                }
                db.SaveChanges();

            }
        }

        public System.Data.DataTable GetReBackOrderReCord(OrderInfoEntity item)
        {
            OrderInfoDAL dal = new OrderInfoDAL();
            return dal.GetReBackOrderReCord(item.OrderProductId);
        }

        public List<OrderBagEntity> GetListByPhotoReamk(string photoRemark)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Tb_OrderBag> list =new List<Tb_OrderBag> ();
                List<Tb_OrderBag_BK> list_bk = new List<Tb_OrderBag_BK>();
                List<OrderBagEntity> colletion = new List<OrderBagEntity>();

                foreach (var pi in db.photoInfo.Where(p => p.Remark.Contains(photoRemark)))
                {
                    if (!pi.orderInfoReference.IsLoaded)
                    {
                        pi.orderInfoReference.Load();
                    }
                    if (!pi.orderInfo.Tb_OrderBagReference.IsLoaded)
                    {
                        pi.orderInfo.Tb_OrderBagReference.Load();
                    }

                    if (list.FindIndex(b=>b.OrderBagId==pi.orderInfo.Tb_OrderBag.OrderBagId)<0)
                    {
                        list.Add(pi.orderInfo.Tb_OrderBag);
                    }
                }

                foreach (var pi in db.photoInfo_BK.Where(p => p.Remark.Contains(photoRemark)))
                {
                    if (!pi.orderInfo_BKReference.IsLoaded)
                    {
                        pi.orderInfo_BKReference.Load();
                    }
                    if (!pi.orderInfo_BK.Tb_OrderBag_BKReference.IsLoaded)
                    {
                        pi.orderInfo_BK.Tb_OrderBag_BKReference.Load();
                    }

                    if (list_bk.FindIndex(b => b.OrderBagId == pi.orderInfo_BK.Tb_OrderBag_BK.OrderBagId) < 0)
                    {
                        list_bk.Add(pi.orderInfo_BK.Tb_OrderBag_BK);
                    }
                }




                foreach (var item in list)
                {
                    OrderBagEntity o = new OrderBagEntity();
                    o.BagCode = item.BagCode;
                    o.BagName = item.BagName;
                    o.CompleteTime = item.CompleteTime;
                    o.FetchTime = item.FetchTime;
                    o.IsEdit = item.EditUserID;
                    o.OrderBagID = item.OrderBagId;
                    o.OrderTime = item.OrderTime;
                    o.PreCompleteTime = OrderBagDAL.GetPreCompleteTimeByOrderBag(o.OrderBagID);
                    o.Priority = item.Priority;
                    o.Remark = item.Remark;
                    if (!item.ShopReference.IsLoaded)
                        item.ShopReference.Load();
                    o.ShopName = item.Shop.shopName;
                    OrderBagStatus estatus = (OrderBagStatus)item.Status;
                    o.StatusValue = item.Status;
                    switch (estatus)
                    {
                        case OrderBagStatus.Doing:
                            o.Status = "正常生产"; break;
                        case OrderBagStatus.UnComplete:
                            o.Status = "未完成"; break;
                        case OrderBagStatus.Eroor:
                            o.Status = "发生错误"; break;
                        case OrderBagStatus.Completed:
                            o.Status = "已完成"; break;
                        case OrderBagStatus.Fetched:
                            o.Status = "已结单"; break;
                        case OrderBagStatus.ProductStop:
                            o.Status = "有产品暂停"; break;
                        case OrderBagStatus.OrderBagStop:
                            o.Status = "取消生产"; break;
                        case OrderBagStatus.WaitPhotoMove:
                            o.Status = "移动照片"; break;
                        case OrderBagStatus.Deleted:
                            o.Status = "已删除"; break;
                        default: o.Status = "未知状态"; break;
                    }
                    int t = item.Type;

                    switch (t)
                    {
                        case 0:
                            o.Type = "正常"; break;
                        default:
                            o.Type = "返单"; break;
                    }
                    if (!item.usersReference.IsLoaded)
                        item.usersReference.Load();
                    o.UserID = item.users.userID;
                    o.UserName = item.users.UserName;
                    colletion.Add(o);
                }

                //备份
                foreach (var item in list_bk)
                {
                    OrderBagEntity o = new OrderBagEntity();
                    o.BagCode = item.BagCode;
                    o.BagName = item.BagName;
                    o.CompleteTime = item.CompleteTime;
                    o.FetchTime = item.FetchTime;
                    o.IsEdit = item.EditUserID;
                    o.OrderBagID = item.OrderBagId;
                    o.OrderTime = item.OrderTime;
                    //o.PreCompleteTime = OrderBagDAL.GetPreCompleteTimeByOrderBag(o.OrderBagID);
                    o.Priority = item.Priority;
                    o.Remark = item.Remark;
                    if (!item.ShopReference.IsLoaded)
                        item.ShopReference.Load();
                    o.ShopName = item.Shop.shopName;
                    OrderBagStatus estatus = (OrderBagStatus)item.Status;
                    o.StatusValue = item.Status;
                    switch (estatus)
                    {
                        case OrderBagStatus.Doing:
                            o.Status = "正常生产"; break;
                        case OrderBagStatus.UnComplete:
                            o.Status = "未完成"; break;
                        case OrderBagStatus.Eroor:
                            o.Status = "发生错误"; break;
                        case OrderBagStatus.Completed:
                            o.Status = "已完成"; break;
                        case OrderBagStatus.Fetched:
                            o.Status = "已结单"; break;
                        case OrderBagStatus.ProductStop:
                            o.Status = "有产品暂停"; break;
                        case OrderBagStatus.OrderBagStop:
                            o.Status = "取消生产"; break;
                        case OrderBagStatus.WaitPhotoMove:
                            o.Status = "移动照片"; break;
                        case OrderBagStatus.Deleted:
                            o.Status = "已删除"; break;
                        default: o.Status = "未知状态"; break;
                    }
                    int t = item.Type;

                    switch (t)
                    {
                        case 0:
                            o.Type = "正常"; break;
                        default:
                            o.Type = "返单"; break;
                    }
                    if (!item.usersReference.IsLoaded)
                        item.usersReference.Load();
                    o.UserID = item.users.userID;
                    o.UserName = item.users.UserName;
                    colletion.Add(o);
                }
                //记录查询结果
                _resultList = colletion;
                return colletion;
            }
        }

        /// <summary>
        /// 获取账单信息
        /// </summary>
        /// <param name="orderbagid"></param>
        /// <returns></returns>
        public SHOP_Bill_Info GetShopBillInfo(int orderbagid)
        {
            int Bill_Detail_Id = 1;     //用于报表中的关联ID
            double Bill_Price = 0;
            SHOP_Bill_Info sbInfo = new SHOP_Bill_Info();
            sbInfo.Result = 0;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Tb_OrderBag orderbag = db.Tb_OrderBag.FirstOrDefault(o=>o.OrderBagId ==orderbagid);
                
                
                if (orderbag != null)
                {
                    if (!orderbag.ShopReference.IsLoaded)
                    {
                        orderbag.ShopReference.Load();
                    }
                    Shop curShop = orderbag.Shop;
                    sbInfo.NeedPrice = curShop.IsNeedPrintAmount;
                    sbInfo = new SHOP_Bill_Info();
                    sbInfo.List_Detail = new List<Bill_Detail_Info>();
                    if (!orderbag.orderInfo.IsLoaded)
                    {
                        orderbag.orderInfo.Load();
                    }

                    foreach (var oi in orderbag.orderInfo)
                    {
                        if (oi.status != (int)OrderInfoStatus.Completed && oi.status != (int)OrderInfoStatus.Error && oi.status != (int)OrderInfoStatus.Stop && oi.status != (int)OrderInfoStatus.Deleted)
                        {
                            oi.WorkOrderFlowList.Load();
                            oi.WorkOrderFlowList.ToList().ForEach(w=>w.tb_TechReference.Load());
                            int outPross=(int)TechType.OutProduct;
                            if (oi.WorkOrderFlowList.Where(w => w.tb_Tech.TechType == outPross).Count() > 0)
                            {
                                sbInfo.Result = 2;//有委外产品并且跳过打单
                            }
                            decimal orderInfoPrice = 0;//此产品的真实价格
                            #region 加载数据库
                            if (!oi.Tb_ProductReference.IsLoaded)
                            {
                                oi.Tb_ProductReference.Load();
                            }
                            if (!oi.Tb_Product.tb_CostTypeReference.IsLoaded)
                            {
                                oi.Tb_Product.tb_CostTypeReference.Load();
                            }
                            if (!oi.Tb_Product.Tb_PorductCategoryReference.IsLoaded)
                            {
                                oi.Tb_Product.Tb_PorductCategoryReference.Load();
                            }
                            if (!curShop.Shop_Product_Price.IsLoaded)
                            {
                                curShop.Shop_Product_Price.Load();
                            }
                            if (!oi.photoInfo.IsLoaded)
                            {
                                oi.photoInfo.Load();
                            }
                            if (!oi.Tb_OrderBagReference.IsLoaded)
                            {
                                oi.Tb_OrderBagReference.Load();
                            }
                            string orderBagName = oi.Tb_OrderBag.BagName.Split('_')[0];

                            #endregion
                            string [] orderproductName = oi.OrderProductName.Split('_');
                            int index = oi.OrderProductName.LastIndexOf("_");

                            string productName = "";
                            if (index > 0)
                            {
                                productName = oi.OrderProductName.Substring(0, index);
                            }
                            else
                            {
                                productName = oi.OrderProductName;
                            }
                            Bill_Detail_Info dbinfo = new Bill_Detail_Info() { BillDetailId = Bill_Detail_Id, Remark = oi.Remark, OrderInfoCode = oi.OrderProductId.ToString(), OrderProductName = productName, CtmName = oi.CtmName, OrderBagName = orderBagName };
                            Bill_Detail_Id++;
                            dbinfo.ListDetail_Detail = new List<Bill_Detail_Detail_Info>();
                            orderInfoPrice = BillBLL.GetOrderPrice(curShop, oi, db, dbinfo);


                            dbinfo.BillDetailId = Bill_Detail_Id;

                            sbInfo.List_Detail.Add(dbinfo);
                            Bill_Detail_Id++;
                            Bill_Price += (double)orderInfoPrice;

                            //记录帐单信息            

                            sbInfo.Shop = new Shop_Info() { Address = curShop.address, ShopId = curShop.ShopId, ShopKeeper = curShop.shopKeeper, ShopName = curShop.shopName, Tel1 = curShop.Tel1, Tel2 = curShop.Tel2, QQ = curShop.QQ, IsNeedPrintUPrice = curShop.IsNeedPrintUPrice == null ? false : (bool)curShop.IsNeedPrintUPrice, Salesman = curShop.Salesman, CreditLevel = curShop.CreditLevel == null ? 0 : (int)curShop.CreditLevel, IsAssignByCustom = curShop.IsAssignByCustom,CheckOutType=curShop.CheckOutType };
                            sbInfo.ShopBalance = (double)curShop.Balance;//指商户的总共欠款额度(包括本帐单)
                            sbInfo.BillCount = Math.Round(Bill_Price, 4, MidpointRounding.ToEven);
                        }
                    }
                }
            }

            return sbInfo;
        }


     


        /// <summary>
        /// 将Bill_Detail转换为要打印的信息
        /// </summary>
        /// <param name="bd"></param>
        /// <param name="curShop"></param>
        /// <param name="bdInfo"></param>
        private void ConvertOrderDetailaToViewModel(Bill_Detail_Info bd, Shop curShop, Bill_Detail_Info bdInfo)
        {
            foreach (var bdd in bd.ListDetail_Detail)
            {
                bdInfo.ListDetail_Detail.Add(new Bill_Detail_Detail_Info()
                {
                    Area = bdd.Area,
                    Count = bdd.Count,
                    DetailName = bdd.DetailName,
                    Num = bdd.Num,
                    Spec = bdd.Spec,
                    Type = bdd.Type,
                    UnitPrice = (bool)(curShop.IsNeedPrintUPrice == null ? false : (bool)curShop.IsNeedPrintUPrice) ? bdd.UnitPrice : 0,
                    Bill_Detail_DetailId = bdInfo.BillDetailId
                });
            }
        }


        public int SaveTicketRecords(int orderbagId ,string shopName,string userName,decimal billcount)
        {
            int billIndex = 0;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                TicketRecords tk = db.TicketRecords.FirstOrDefault(t=>t.OrderBagId ==orderbagId);
                if (tk != null)
                {
                    billIndex = tk.TicketIndex;
                }
                else
                {
                    billIndex = 1;
                    List<TicketRecords> ticketList =db.TicketRecords.ToList();
                    if (ticketList.Count > 0)
                    {
                        billIndex = db.TicketRecords.ToList().Max(t => t.TicketIndex) + 1;
                    }
                    TicketRecords ticketrecords = new TicketRecords()
                    {
                        BillCount = billcount,
                        OrderBagId = orderbagId,
                        PrintTime = DateTime.Now,
                        PrintUserName = userName,
                        ShopName = shopName,
                        TicketIndex = billIndex
                    };
                    db.AddToTicketRecords(ticketrecords);
                }
                db.SaveChanges();
            }
            return billIndex;
        }

        public bool EditEnable(int userId, int orderproductId)
        {
            return true;
        }

    }

    #region //OrderBag实体类
    public class OrderBagEntity2
    {
        public int OrderBagID { get; set; }
        public string BagCode { get; set; }
        public string BagName { get; set; }
        public string ShopName { get; set; }
        public DateTime OrderTime { get; set; }
        public DateTime FetchTime { get; set; }
        public Nullable<DateTime> CompleteTime { get; set; }
        public DateTime PreCompleteTime { get; set; }
        public int Priority { get; set; }
        public string Remark { get; set; }
    }
    public class OrderBagEntity3
    {
        private Tb_OrderBag _obj = null;

        public bool IsDirty = false;

        public OrderBagEntity3()
        {

        }

        public OrderBagEntity3(Tb_OrderBag orderbag)
        {
            this._obj = orderbag;
        }


        public int OrderBagID
        {
            get
            {
                return this._obj.OrderBagId;
            }
        }

        public string BagCode
        {
            get
            {
                return this._obj.BagCode;
            }

        }

        public string BagName
        {
            get
            {
                return this._obj.BagName;
            }
            set
            {
                if (this._obj.BagName != value)
                {
                    this.IsDirty = true;
                    this._obj.BagName = value;
                }
            }
        }

        public string ShopName
        {
            get
            {
                if (this._obj.Shop != null)
                {
                    return this._obj.Shop.shopName;
                }

                return string.Empty;
            }
        }

        public DateTime OrderTime
        {
            get
            {
                return this._obj.OrderTime;
            }
            set
            {
                if (this._obj.OrderTime != value)
                {
                    this.IsDirty = true;
                    this._obj.OrderTime = value;
                }
            }
        }

        public DateTime FetchTime
        {
            get
            {
                return this._obj.FetchTime;
            }
            set
            {
                if (this._obj.FetchTime != value)
                {
                    this.IsDirty = true;
                    this._obj.FetchTime = value;
                }
            }
        }

        public Nullable<DateTime> CompleteTime
        {
            get
            {
                if (this.StatusValue == (int)OrderBagStatus.Completed)
                    return this._obj.CompleteTime;

                return null;
            }
        }

        public DateTime PreCompleteTime
        {
            get
            {
                return OrderBagDAL.GetPreCompleteTimeByOrderBag(this._obj.OrderBagId);
            }
        }

        public string Status
        {
            get
            {
                return this.GetStatusStr(this._obj.Status);
            }
        }

        public int StatusValue
        {
            get
            {
                return this._obj.Status;
            }
            set
            {
                this._obj.Status = value;
            }
        }

        private string GetStatusStr(int status)
        {
            OrderBagStatus estatus = (OrderBagStatus)status;
            switch (estatus)
            {
                case OrderBagStatus.Doing:
                    return "正常生产";
                case OrderBagStatus.UnComplete:
                    return "未完成";
                case OrderBagStatus.Eroor:
                    return "发生错误";
                case OrderBagStatus.Completed:
                    return "已完成";
                case OrderBagStatus.Fetched:
                    return "已结单";
                case OrderBagStatus.ProductStop:
                    return "有产品暂停";
                case OrderBagStatus.OrderBagStop:
                    return "取消生产";
            }

            return "未知状态";
        }

        public int Priority
        {
            get
            {
                return this._obj.Priority;
            }
            set
            {
                if (this._obj.Priority != value)
                {
                    this.IsDirty = true;
                    this._obj.Priority = value;
                }
            }
        }

        public string Type
        {
            get
            {
                if (this._obj.Type == 0)
                {
                    return "正常";
                }
                else
                {
                    return "返单";
                }
            }
        }

        public int IsEdit
        {
            get
            {
                return this._obj.EditUserID;
            }
            set
            {
                if (this._obj.EditUserID != value)
                {
                    this._obj.EditUserID = value;
                }
            }
        }

        public string Remark
        {
            get
            {
                return this._obj.Remark;
            }
            set
            {
                if (this._obj.Remark != value)
                {
                    this.IsDirty = true;
                    this._obj.Remark = value;
                }
            }
        }

        public string UserName
        {
            get
            {
                if (this._obj.users != null)
                    return this._obj.users.UserName;

                return string.Empty;
            }
        }

        public int UserID
        {
            get
            {
                if (this._obj.users != null)
                {
                    return this._obj.users.userID;
                }

                return -1;
            }
        }


        internal Tb_OrderBag Obj
        {
            get
            {
                return this._obj;
            }
        }

    }

    public class OrderBagEntity
    {
        public int OrderBagID
        {
            get;
            set;
        }

        public string BagCode
        {
            get;
            set;
        }

        public string BagName
        {
            get;
            set;
        }

        public string ShopName
        {
            get;
            set;
        }

        public DateTime OrderTime
        {
            get;
            set;
        }

        public DateTime FetchTime
        {
            get;
            set;
        }

        public Nullable<DateTime> CompleteTime
        {
            get;
            set;
        }

        public DateTime PreCompleteTime
        {
            get;
            set;
        }

        public string Status
        {
            get;
            set;
        }

        public int StatusValue
        {
            get;
            set;
        }

        private string GetStatusStr(int status)
        {
            OrderBagStatus estatus = (OrderBagStatus)status;
            switch (estatus)
            {
                case OrderBagStatus.Doing:
                    return "正常生产";
                case OrderBagStatus.UnComplete:
                    return "未完成";
                case OrderBagStatus.Eroor:
                    return "发生错误";
                case OrderBagStatus.Completed:
                    return "已完成";
                case OrderBagStatus.Fetched:
                    return "已结单";
                case OrderBagStatus.ProductStop:
                    return "有产品暂停";
                case OrderBagStatus.OrderBagStop:
                    return "取消生产";
                case OrderBagStatus.WaitPhotoMove:
                    return "移动照片";
                case OrderBagStatus.Deleted:
                    return "删除";
                case OrderBagStatus.AutoStartOrder:
                    return "自动开单";
            }

            return "未知状态";
        }

        public int Priority
        {
            get;
            set;
        }

        public string Type
        {
            get;
            set;
        }

        public int IsEdit
        {
            get;
            set;
        }

        public string Remark
        {
            get;
            set;
        }

        public string UserName
        {
            get;
            set;
        }

        public int UserID
        {
            get;
            set;
        }

    }


    /// <summary>
    /// 停止、恢复产品记录的结构
    /// </summary>
    public class StopRecord
    {
        public string OrderBagCode { get; set; }
        public string OrderName { get; set; }
        public string OrderProductCode { get; set; }
        public string OrderProductName { get; set; }
        public string TechName { get; set; }
        public string WorkerName { get; set; }
        public string OrderProductStatus { get; set; }      //是"停止"还是"恢复"
        public string Remark { get; set; }
        public DateTime CreateTime { get; set; }
    }

    #endregion

    /// <summary>
    /// 快速查询条件类
    /// </summary>
    public class RapidCondition
    {
        //public int status = -1;
        public string bagName = string.Empty;
        public string bagCode = string.Empty;
        public DateTime orderStart = DateTime.MinValue;
        public DateTime orderEnd = DateTime.MaxValue;
        public int priority = -1;
        public DateTime fetchStart = DateTime.MinValue;
        public DateTime fetchEnd = DateTime.MinValue;
        public int flowList = -1;
        public int userID = -1;
    }

}
